// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.property;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dengues.core.components.IXMLComponent;
import org.dengues.core.components.NodePropertyParameter;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-29 qiang.zhang $
 * 
 */
public class PropertyCellModifier implements ICellModifier {

    private NodePropertyParameter param;

    private String[] propertyIDs;

    private TableViewer tableViewer;

    private List<EParameterFieldCategory> categories;

    private CommandStack commandStack;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
     */
    public boolean canModify(Object element, String property) {
        if (property.equals(PropertyTableViewer.SPACES)) {
            return false;
        } else if (param.isBaseSchema() && IXMLComponent.COLUMN_SCHEMA.equals(property)) {
            return false;
        }
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
     */
    public Object getValue(Object element, String property) {
        for (int i = 0; i < propertyIDs.length; i++) {
            String string = propertyIDs[i];
            if (string.equals(property)) {
                EParameterFieldCategory fieldCategory = categories.get(i);
                switch (fieldCategory) {
                case COMBO:
                    return new Integer(i);
                case CHECK:
                    return ((Map) element).get(param.getItemNames()[i - 1]);
                default:
                    if (element instanceof Map) {
                        return ((Map) element).get(param.getItemNames()[i - 1]).toString();
                    }
                }
            }
        }
        return ""; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
     */
    public void modify(Object element, String property, Object value) {
        TableItem tableItem = ((TableItem) element);
        element = tableItem.getData();
        if (value instanceof Boolean) {
            value = !((Boolean) ((Map) element).get(property));
        }
        ((Map) element).put(property, value);
        if (getCommandStack() != null) {
            getCommandStack().execute(new Command() {
            });
        }
        tableViewer.refresh();
    }

    /**
     * Sets the param.
     * 
     * @param param the param to set
     */
    public void setParam(NodePropertyParameter param) {
        this.param = param;
        categories = new ArrayList<EParameterFieldCategory>();
        getCategories().add(EParameterFieldCategory.TEXT);
        Object[] itemValues = param.getItemValues();
        for (int i = 0; i < itemValues.length; i++) {
            NodePropertyParameter parameter = (NodePropertyParameter) itemValues[i];
            getCategories().add(parameter.getFieldCategory());
        }
        int length = param.getItemNames().length;
        propertyIDs = new String[length + 1];
        propertyIDs[0] = PropertyTableViewer.SPACES;
        System.arraycopy(param.getItemNames(), 0, propertyIDs, 1, length);
    }

    /**
     * Sets the tableViewer.
     * 
     * @param tableViewer the tableViewer to set
     */
    public void setTableViewer(TableViewer tableViewer) {
        this.tableViewer = tableViewer;
    }

    /**
     * Getter for categories.
     * 
     * @return the categories
     */
    public List<EParameterFieldCategory> getCategories() {
        return this.categories;
    }

    /**
     * Getter for commandStack.
     * 
     * @return the commandStack
     */
    public CommandStack getCommandStack() {
        return this.commandStack;
    }

    /**
     * Sets the commandStack.
     * 
     * @param commandStack the commandStack to set
     */
    public void setCommandStack(CommandStack commandStack) {
        this.commandStack = commandStack;
    }

}
